package org.skyeye.modeling.customerized.model;

import org.eclipse.draw2d.geometry.Dimension;

abstract public class SimpleInput extends MGPIONode {

	static final long serialVersionUID = 1;
	protected boolean value = false;
	private static int count;
	public static String TERMINAL_IN = "IN";
	public static String P_VALUE = "value";

	public String getNewID() {
		return Integer.toString(count++);
	}

	public Object getPropertyValue(Object propName) {
		if (ID_SIZE.equals(propName)) {
			return new String("(" + getSize().width + "," + getSize().height + ")");//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
		}
		return super.getPropertyValue(propName);
	}

	abstract public boolean getResult();

	public Dimension getSize() {
		return new Dimension(-1, -1);
	}

	/**
	 * Nulls out any changes to this and its subclasses as they are of fixed
	 * size.
	 */
	public void setPropertyValue(Object id, Object value) {
		if (ID_SIZE.equals(id))
			super.setPropertyValue(id, new Dimension(getSize()));
		else
			super.setPropertyValue(id, value);
	}

	protected boolean hasInput() {
		return !inputs.isEmpty();
	}

	public void update() {

		if (hasInput()) {
			value = getInput(TERMINAL_IN);
			setOutput(TERMINAL_IN, getInput(TERMINAL_IN));
			firePropertyChange(P_VALUE, null, value);
		} else {
			setOutput(TERMINAL_IN, false);
			firePropertyChange(P_VALUE, null, false);
		}
	}

	public boolean getValue() {
		return value;
	}

	public void setValue(boolean val) {
		this.value = val;
		this.update();
	}

}
